package com.sugar.subscribe.course.module.business.service.city;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import com.sugar.subscribe.course.module.business.controller.admin.city.vo.CityPageReqVO;
import com.sugar.subscribe.course.module.business.controller.admin.city.vo.CityRespVO;
import com.sugar.subscribe.course.module.business.controller.admin.city.vo.CitySaveReqVO;
import com.sugar.subscribe.course.module.business.dal.dataobject.city.CityDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.venue.VenueDO;
import com.sugar.subscribe.course.module.business.dal.mysql.city.CityMapper;
import com.sugar.subscribe.course.module.business.dal.mysql.venue.VenueMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.CITY_NOT_EXISTS;

/**
 * 城市 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class CityServiceImpl implements CityService {

    @Resource
    private CityMapper cityMapper;
    @Resource
    private VenueMapper venueMapper;

    @Override
    public Long createCity(CitySaveReqVO createReqVO) {
        // 插入
        CityDO city = BeanUtils.toBean(createReqVO, CityDO.class);
        cityMapper.insert(city);

        // 返回
        return city.getId();
    }

    @Override
    public void updateCity(CitySaveReqVO updateReqVO) {
        // 校验存在
        getCityValidate(updateReqVO.getId());
        // 更新
        CityDO updateObj = BeanUtils.toBean(updateReqVO, CityDO.class);
        cityMapper.updateById(updateObj);

        // 更新该城市下场地冗余的城市名称
        venueMapper.update(VenueDO::getCityId, updateObj.getId(), VenueDO::getCityName, updateObj.getName());
    }

    @Override
    public void deleteCity(Long id) {
        // 校验存在
        getCityValidate(id);
        // 删除
        cityMapper.deleteById(id);
    }

    @Override
    public void deleteCityListByIds(List<Long> ids) {
        // 删除
        cityMapper.deleteByIds(ids);
    }


    @Override
    public CityDO getCityValidate(Long id) {
        CityDO cityDO = cityMapper.selectById(id);
        if (cityDO == null) {
            throw exception(CITY_NOT_EXISTS);
        }
        return cityDO;
    }

    @Override
    public CityDO getCity(Long id) {
        return cityMapper.selectById(id);
    }

    @Override
    public PageResult<CityDO> getCityPage(CityPageReqVO pageReqVO) {
        return cityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CityRespVO> getCityList() {
        return BeanUtils.toBean(cityMapper.selectList(Wrappers.<CityDO>lambdaQuery().orderByDesc(CityDO::getSort)), CityRespVO.class);
    }

}